/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcProduceService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 生产收货
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/prd_col")
@RestController
public class XqcPrdColController {

    /** 生产服务 */
    @Autowired
    private IXqcProduceService produceService;

    /**
     * 保存生产收货单
     */
    @PostMapping("/upload_prdcol_bill")
    @VtuzxApi
    public Object uploadPrdColBill(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("file") MultipartFile[] files) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        produceService.uploadPrdColBill(_userId, name, vFileList.get(0).file());
        return null;
    }

    /**
     * 保存生产收货单
     */
    @PostMapping("/save_prdcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "materialNo", label = "物料", notNull = true),
            @VtuzxValidator(key = "batchNo", label = "批号", notNull = true),
            @VtuzxValidator(key = "produceDate", label = "生产日期", notNull = true),
            @VtuzxValidator(key = "prdCount", label = "总重", notNull = true),
    } )
    public Object savePrdColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return produceService.savePrdColBill(_userId, name, param);
    }

    /**
     * 保存生产收货单(供应商)
     */
    @PostMapping("/save_prdcol_bill_supplier")
    @VtuzxApi
    public Object savePrdColBillSupplier(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        produceService.savePrdColBillSupplier(_userId, name, param);
        return null;
    }

    /**
     * 保存生产收货单(干参)
     */
    @PostMapping("/save_simpleCol_bill")
    @VtuzxApi
    public Object saveSimpleColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        produceService.saveSimpleColBill(_userId, name, param);
        return null;
    }

    /**
     * 保存生产收货单
     */
    @PostMapping("/delete_prdcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object deletePrdColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token,"name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        produceService.deletePrdColBill(_userId,userName, billNo);
        return null;
    }

    /**
     * 审核生产收货单
     */
    @PostMapping("/audit_prdcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object auditPrdColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        String name = VtuzxUtil.getString(token, "name");
        return produceService.auditPrdColBill(_userId, name, billNo);
    }

    /**
     * 反审核生产收货单
     */
    @PostMapping("/unaudit_prdcol_bill")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object unauditPrdColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token,"name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        return produceService.unauditPrdColBill(_userId,userName, billNo);
    }

    /**
     * 根据RFID从生产收货单中获取物料
     */
    @PostMapping("/get_material_by_rfid")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "rfid", label = "RFID", notNull = true)
    } )
    public Object getMaterialByRfid(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> rfidList = VtuzxUtil.getObject(param, "rfid");
        return produceService.getPrdColMaterialByRfid(rfidList);
    }

    /**
     * 根据箱号从生产收货单中获取物料
     */
    @PostMapping("/get_material_by_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object getMaterialByBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        String boxNo= VtuzxUtil.getString(param, "boxNo");
//        String boxNo;
//        if (boxNoKey.contains("|;")) {
//            boxNo = boxNoKey.substring(boxNoKey.lastIndexOf("|;") + 2);
//        } else {
//            boxNo = boxNoKey;
//        }
        return produceService.getPrdColMaterial(boxNo);
    }

    /**
     * 获取生产收货单
     */
    @PostMapping("/get_prdcol_bill")
    @VtuzxApi
    public Object getPrdColBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return produceService.getPrdColBill(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取生产收货单详细
     */
    @PostMapping("/get_prdcol_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object getPrdColBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return produceService.getPrdColBillDetail(_userId, billNo);
    }

    /**
     * 下载生产收货单列表(用于打印二维码)
     */
    @PostMapping("/download_prdcol_bill_barcode")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNo", label = "单号", notNull = true)
    } )
    public Object downloadPrdColBillBarcode(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String billNo = VtuzxUtil.getString(param, "billNo");
        return produceService.downloadPrdColBillBarcode(_userId, billNo);
    }

    /**
     * 下载生产收货单列表(多单据, 用于打印二维码)
     */
    @PostMapping("/download_prdcol_bill_barcode_list")
    @VtuzxApi(type = ApiType.Download, fields = {
            @VtuzxValidator(key = "billNoList", label = "单号列表", notNull = true)
    } )
    public Object downloadPrdColBillBarcodeList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        return produceService.downloadPrdColBillBarcodeList(_userId, billNoList);
    }


    /**
     * 返回生产任务单号
     */
    @PostMapping("/get_prd_task_billNo")
    @VtuzxApi
    public Object getPrdTaskBillNo(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        return produceService.getPrdTaskBillNo(_userId, name, key, pageIndex, pageSize);
    }

    /**
     * 批量导出
     */
    @PostMapping("/get_prd_col_batch")
    @VtuzxApi
    public Object getPrdColBatch(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return produceService.getPrdColBatch(_userId, name, param);
    }



    /**
     * 获取收货单（边，董，潘）
     */
    @PostMapping("/get_prdCol_part_bill")
    @VtuzxApi
    public Object getPrdColPartBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return produceService.getPrdColPartBill(_userId, param, pageIndex, pageSize);
    }
    /**
     * 下载车间收货(库管)
     */
    @PostMapping("/download_prdCol_part_bill")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadPrdColPartBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return produceService.downloadPrdColPartBill(token, param);
    }

    @PostMapping("/update_prd_col_seal")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNoList", label = "单号", notNull = true)
    } )
    public Object updatePrdColSeal(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return produceService.updatePrdColSeal(_userId, name, param);
    }


    /**
     * 校验收货批次
     */
    @PostMapping("/check_prd_col_batch")
    @VtuzxApi()
    public Object checkPrdColBatch(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return produceService.checkPrdColBatch(_userId, name, param);
    }
}
